package com.managertrade.util.strategy;

import com.managertrade.dto.vo.KlineWithIndicators;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class TrendAnalyzerUtils {

    /**
     * 判断实际方向：结合多种技术指标进行综合评估
     * @param klineList 包含技术指标的 K 线数据列表，假设为时间升序排列
     * @return "涨" | "跌" | "平稳"
     */
    public static String calculateActualDirection(List<KlineWithIndicators> klineList) {
        if (klineList == null || klineList.size() < 2) {
            return "平稳";
        }

        KlineWithIndicators first = klineList.get(0);
        KlineWithIndicators last = klineList.get(klineList.size() - 1);

        double priceChange = last.getClose() - first.getClose();
        double priceChangePercent = priceChange / first.getClose();

        // 价格变化主判断
        if (Math.abs(priceChangePercent) < 0.005) {
            return "平稳";
        }

        // EMA 判断
        boolean emaBullish = last.getEma7() != null && last.getEma21() != null && last.getEma7() > last.getEma21();
        boolean emaBearish = last.getEma7() != null && last.getEma21() != null && last.getEma7() < last.getEma21();

        // MACD 判断
        boolean macdPositive = last.getHistogram() != null && last.getHistogram() > 0;
        boolean macdNegative = last.getHistogram() != null && last.getHistogram() < 0;

        // BOLL 判断
        boolean closeAboveMiddle = last.getClose() > (last.getBollMiddle() != null ? last.getBollMiddle() : last.getClose());
        boolean closeBelowMiddle = last.getClose() < (last.getBollMiddle() != null ? last.getBollMiddle() : last.getClose());

        int bullishScore = 0;
        int bearishScore = 0;

        if (emaBullish) bullishScore++;
        if (macdPositive) bullishScore++;
        if (closeAboveMiddle) bullishScore++;

        if (emaBearish) bearishScore++;
        if (macdNegative) bearishScore++;
        if (closeBelowMiddle) bearishScore++;

        if (bullishScore >= 2) return "涨";
        if (bearishScore >= 2) return "跌";

        return "平稳";
    }
}
